package com.example.goodpet.my.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.db.entity.GoodpetHistory;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.bean.UserPetInfoAndHistory;
import com.example.goodpet.utils.Constants;

import java.util.List;

public class PetHistoryAdapter extends RecyclerView.Adapter<PetHistoryAdapter.ViewHolder>{
    private List<UserPetInfoAndHistory> lists;
    private Context context;

    public PetHistoryAdapter(List<UserPetInfoAndHistory> lists, Context context) {
        this.lists = lists;
        this.context = context;
    }

    @NonNull
    @Override
    public PetHistoryAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_history, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull PetHistoryAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        UserPetInfoAndHistory userPetInfoAndHistory = lists.get(position);

        System.out.println("PetHistoryAdapter.onBindViewHolder"+userPetInfoAndHistory.toString());

        UserPetInfo userPetInfo = userPetInfoAndHistory.getUserPetInfo();
        GoodpetHistory goodpetHistory = userPetInfoAndHistory.getGoodpetHistory();
        Glide.with(context).load(Constants.BASE_URL_IMAGE+userPetInfo.getPetImage().get(0)).into(holder.petImage);
        holder.overview.setText(userPetInfo.getOverview());
        holder.collectTime.setText(goodpetHistory.getPostdate().toString());
        holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PetInfoActivity.class);
                intent.putExtra("userPetInfo",lists.get(position).getUserPetInfo());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView petImage;
        private TextView overview;
        private TextView collectTime;
        private LinearLayout layoutEnter;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            petImage=itemView.findViewById(R.id.petImage);
            overview=itemView.findViewById(R.id.overview);
            collectTime=itemView.findViewById(R.id.collectTime);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);
        }
    }
}
